<?php

namespace app\admin\controller;

use app\common\controller\Send;
use app\common\enums\StatusEnum;
use think\facade\Request;
use app\admin\model\Coupon AS CouponModel;
use app\admin\validate\Coupon AS CouponValidate;

class Coupon extends Base
{
    use Send;

    public function common()
    {
        $search = input('');
        $oauthId = isset($search['oauth_id']) ? $search['oauth_id'] : 0;
        $name = isset($search['name']) ? $search['name'] : '';
        $status = isset($search['status']) ? $search['status'] : -1;
        $type = isset($search['type']) ? $search['type'] : -1;
        $search = [
            'oauth_id' => $oauthId,
            'name' => $name,
            'status' => $status,
            'type' => $type
        ];
        $where = [];
        if ($search['name']) {
            $where[] = ['name', 'like', '%' . $search['name'] . '%'];
        }

        if ($search['status'] != null && $search['status'] != -1) {
            $where[] = ['status', '=', $search['status']];
        }

        $model = CouponModel::getList($where, $oauthId, $type);
        return [
            'model' => $model['model'],
            'count' => $model['count'],
            'search' => $search,
            'status' => StatusEnum::$listExplain,
            'type' => CouponModel::attributes('type'),
        ];
    }

    /**
     * 列表
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $data = $this->common();
        $data['cancelUrl'] = url('index');
        return view('index', $data);
    }

    /**
     * 我的优惠券
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function mine()
    {
        $data = $this->common();
        $data['cancelUrl'] = url('mine', ['oauth_id' => $data['search']['oauth_id']]);
        return view('mine', $data);
    }

    /**
     * 添加修改
     * @return \think\response\Json|\think\response\View
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function form()
    {
        if (Request::instance()->isPost()) {
            $data = input('post.');

            // 检测变量
            $validate = new CouponValidate();
            if (!$validate->check($data)) {
                return json(self::sendResult(102, [], $validate->getError()));
            }

            $langId = CouponModel::saveOne($data);
            $code = 602;
            if ($langId > 0) {
                $code = self::$okCode;
            }
            return json(self::sendResult($code, [
                'url' => url('index'),
            ]));
        } else {
            $id = input('id') ? input('id') : 0;
            $model = [];
            $title = '添加优惠券';
            if ($id > 0) {
                $title = '编辑优惠券';
                $model = CouponModel::get($id);
                $model['start_time'] = get_datetime($model['start_time']);
                $model['end_time'] = get_datetime($model['end_time']);
            }
            return view('form', [
                'status' => StatusEnum::$listExplain,
                'model' => $model,
                'title' => $title
            ]);
        }
    }

    /**
     * 删除
     * @param $id
     * @return array
     */
    public function delete($id)
    {
        CouponModel::deleteOne($id);
        return self::sendResult(self::$okCode, [
            'url' => url('index'),
        ]);
    }

    /**
     * 批量启用、禁用
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function disables()
    {
        $status = input('status');
        $ids = input('ids');
        CouponModel::disables($ids, $status);
        return self::sendResult(self::$okCode, [
            'url' => url('index'),
        ]);
    }

    /**
     * 修改状态
     * @param $id
     * @return array
     */
    public function disabled($id)
    {
        CouponModel::disabledOne($id);
        return self::sendResult(self::$okCode, [
            'url' => url('index'),
        ]);
    }
}
